
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.http.HttpRequest;
import com.smdk.dsminio.vo.MultipartFileParam;

import java.io.File;
import java.io.FileInputStream;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

public class FileUploadOne{


    public static void upload(String fileId, int num, File file, Long partSize, Long partStart, Long partCount, String fileMd5) {
        try{
            FileInputStream fis = new FileInputStream(file);
            MultipartFileParam multipartFileParam =new MultipartFileParam();
            //丢弃起始位置 读过的直接可以不要了 免得浪费资源
            fis.skip(partStart);
            byte[] fileByte=new byte[partSize.intValue()];
            //不能用while判断
            fis.read(fileByte,0,partSize.intValue());
            multipartFileParam.setFileBaseStr(Base64.encode(fileByte));
            multipartFileParam.setBucketId(1L);
            multipartFileParam.setChunk(num);
            multipartFileParam.setChunks(partCount.intValue());
            multipartFileParam.setParentFolderId(1L);
            multipartFileParam.setSize(file.length());
            multipartFileParam.setUid(fileId);
            multipartFileParam.setId("0");
            multipartFileParam.setMd5(fileMd5);
            multipartFileParam.setFileName(file.getName());
            Map paramMap= BeanUtil.beanToMap(multipartFileParam);
            System.out.println("开始上传分块:"+num);
            //Gateway网关
            String response = HttpRequest.post("http://w2pb2wgp7ebe6x1:9008/api-a/api/fileNodeDistribute").form(paramMap).timeout(5000).execute().body();
            System.out.println(response);
            System.out.println("分块"+num+"上传完毕");

//            countDownLatch.countDown();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}